package com.example.demo.lc.dp;

/**
* @author wzh
* @date 2021/7/20 8:01 下午
* @Description     38. 外观数列
 *https://leetcode-cn.com/problems/count-and-say/
*/
public class LeetCode38 {
    public static void main(String[] args) {
        //11
        //21
        //1211
        System.out.println(countAndSay(6));
    }

    public static String countAndSay(int n) {
        String[] dp = new String[n];
        dp[0] = "1";
        for (int i = 1; i < n; i++) {
            StringBuilder ssb = new StringBuilder();
            char[] chars = dp[i - 1].toCharArray();
            char part = chars[0];
            for (int j = 0; j < chars.length; j++) {
                int k = 0;
                part = chars[j];
                while(j < chars.length && chars[j] == part){
                    k++;
                    j++;
                }
                ssb.append(k).append(part);
                j--;
            }
            dp[i] = ssb.toString();
        }
        return dp[n-1];
    }

    //1
    // 11
    // 211
}
